﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using WeChatParking.Entity;

namespace WeChatParking.Core.BLL
{
    public class Role
    {
        public static List<Entity.Role> Get(string filter, string OrderFields = "", bool isDesc = false)
        {
            using (var dbContext = Repository.DbContext.Create())
            {
                try
                {
                    string sql = "select * from Role";
                    if (filter.Length > 0)
                        sql += "  where  " + filter;
                    if (OrderFields.Length > 0)
                    {
                        if (isDesc)
                            sql += "   ORDER BY  " + OrderFields + "  DESC";

                        else
                            sql += "   ORDER BY  " + OrderFields;
                    }
                    return dbContext.GetListBySql<Entity.Role>(sql, null).ToList();
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex, "获取角色数据");
                    return null;
                }
            }
        }
        public Entity.Role Get(int ID)
        {
            using (var dbContext = Repository.DbContext.Create())
            {
                return dbContext.Get<Entity.Role>(ID, null);
            }
        }
        /// <summary>
        /// 获取角色信息
        /// </summary>
        /// <param name="enter_id">企业id</param>
        /// <param name="role_name">角色名</param>
        /// <param name="filter"></param>
        /// <returns></returns>
        public PageList<Entity.RoleQuery> GetRoleQuery(int enter_id, string role_name, Entity.BaseFilter filter)
        {
            dynamic predicate = new ExpandoObject();
            string sql = @"   SELECT r.ID AS role_id , r.RoleName AS role_name ,e.Enterprise_Name AS enter_name ,r.create_time,r.create_user,r.update_time,r.update_user FROM dbo.Role r LEFT JOIN Enterprise e  ON   r.EnterpriseID=e.ID where r.RoleName LIKE '%{0}%'  ";
            sql = String.Format(sql, role_name);
            sql = enter_id > 0 ? sql + " and r.EnterpriseID= " + enter_id : sql;
            sql += " ORDER BY r." + filter.sort + (filter.asc ? string.Empty : "  DESC");
            using (var dbContext = Repository.DbContext.Create())
            {

                var data = dbContext.GetListBySql<Entity.RoleQuery>(sql, predicate, filter.page - 1, filter.rows);
                return data;
            }
        }

        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name">名</param>
        /// <param name="openUser">操作用户</param>
        /// <returns></returns>
        public string Update(int id, string name, string openUser)
        {
            if (!this.Exists(name))
            {
                var role = this.Get(id);
                role.RoleName = name;
                role.update_user = openUser;
                role.update_time = DateTime.Now;
                using (var dbContext = Repository.DbContext.Create())
                {
                    if (dbContext.Update(role))
                        return "";
                    else
                        return "失败";

                }
            }
            else return "已经存在该角色名";
        }
        /// <summary>
        /// 更新角色权限
        /// </summary>
        /// <param name="d"></param>
        /// <param name="RoleID"></param>
        /// <returns></returns>
        public string UpdatePermission(Dictionary<int, bool> d, int RoleID, string openUser)
        {
            var role = this.Get(RoleID);
            if (role == null) return "失败";
            string PermissionID = "";
            foreach (var item in d)
            {
                PermissionID += item.Value ? item.Key.ToString() + "," : "";
            }
            PermissionID = PermissionID.TrimEnd(',');
            role.PermissionID = PermissionID;
            role.update_time = DateTime.Now;
            using (var dbContext = Repository.DbContext.Create())
            {
                if (dbContext.Update(role))
                    return "";
                else
                    return "失败";

            }
        }
        /// <summary>
        /// 是否存在名的角色
        /// </summary>
        /// <param name="RoleName"></param>
        /// <returns></returns>
        public bool Exists(string RoleName)
        {
            using (var dbContext = Repository.DbContext.Create())
            {
                var precidate = new { RoleName = RoleName };
                return dbContext.Count<Entity.Role>(precidate, null) > 0;
            }
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="role"></param>
        /// <param name="EnterpriseID">企业ID</param>
        /// <param name="openUser">操作用户</param>
        /// <returns></returns>
        public int Add(Entity.RoleQuery role, int EnterpriseID, string openUser)
        {
            int newId = -1;

            if (this.Exists(role.role_name))
                return newId;

            Entity.Role model = new Entity.Role();
            model.PermissionID = "1";
            model.RoleName = role.role_name;
            model.EnterpriseID = EnterpriseID;
            model.update_time = DateTime.Now;
            model.update_user = openUser;
            model.create_time = DateTime.Now;
            model.create_user = openUser;
            using (var dbContext = Repository.DbContext.Create())
            {
                newId = (int)dbContext.Insert<Entity.Role>(model);
            }
            return newId;
        }
        /// <summary>
        ///删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public string Delete(int Id)
        {

            using (var dbContext = Repository.DbContext.Create())
            {
                var predicate = new { RoleID = Id };
                var userCnt = dbContext.Count<Entity.User_info>(predicate, null);
                if (userCnt > 0) return "用户不为空";
                var predicate2 = new { ID = Id };
                bool success = dbContext.Delete<Entity.Role>(predicate2, null);
                if (success)
                    return string.Empty;
                return "删除失败";
            }
        }

    }
}
